package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.kedou.sdk.assist.Constant;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;
import com.kedou.sdk.ui.view.DialogFactory;
import com.kedou.sdk.utils.ResUtils;
import com.kedou.sdk.utils.ToolsUtils;
import com.kedou.sdk.utils.WL;

/**
 * Created by Administrator on 2018/3/7.
 */

public class RealNameDialogFragment extends BaseDialogFragment implements View.OnClickListener , TextWatcher {

    private static final String TAG = RealNameDialogFragment.class.getSimpleName();

//    private ImageButton mCloseWindowBtn;
//    private TextView mTitleTextView,mSubTitleTextView;
    private EditText mIdentificationNameEt;
    private EditText mIdentificationCardEt;

    private Button mBindPhoneBtn;
//    private ExViewPager mExViewPager;

//    private LoginBindPagerAdapter mPagerAdapter;

    private ForceRealNameCertifiesBroadcastReceiver mBroadcastReceiver;

    public static RealNameDialogFragment newInstance(Bundle bundle) {
        RealNameDialogFragment fragment = new RealNameDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        onInitViews();
//
//        mReceiver = new ExtensionBarBroadcastReceiver();
//        IntentFilter fliter = new IntentFilter(Constant.LocalIntentAction.DESTROY_FLOAT_EXTENSIONBAR);
//        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, fliter);
//    }

//    @Override
//     void onDestroy() {
//        super.onDestroy();
//        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
//    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isNullContext()) {
            mBroadcastReceiver = new ForceRealNameCertifiesBroadcastReceiver();
            IntentFilter filter = new IntentFilter(Constant.LocalIntentAction.INTENT_AACTION_FORCE_REALNAME_CERTIFIES);
            LocalBroadcastManager.getInstance(getContext()).registerReceiver(mBroadcastReceiver, filter);
        } else {
            WL.i(TAG, "onCreate ========> Context is null ......");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!isNullContext()) {
            LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mBroadcastReceiver);
        } else {
            WL.i(TAG, "onDestroy ========> Context is null ......");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("hnssn_activity_bind_realname"));
        mIdentificationNameEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bind_identification_name"));
        mIdentificationCardEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bind_identification_card"));
        mBindPhoneBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_submit_btn"));

        mBindPhoneBtn.setOnClickListener(this);

        mIdentificationNameEt.addTextChangedListener(this);
        mIdentificationCardEt.addTextChangedListener(this);
        mIdentificationCardEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE&&mBindPhoneBtn.isEnabled()){
                    doBindIdentityCardNumber();
                }
                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
                return false;
            }
        });
//        mExViewPager = contentView.findViewById(getViewIdentifier("activity_bindphone_viewpager"));
//        mTitleTextView = contentView.findViewById(getViewIdentifier("activity_bindphone_title_name"));
//        mSubTitleTextView = contentView.findViewById(getViewIdentifier("activity_bindphone_title_subtitle"));


        return contentView;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkSumbitButtonState();
    }

    @Override
    public void afterTextChanged(Editable s) {}

    private void checkSumbitButtonState() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(idCardNumber)) {
            mBindPhoneBtn.setEnabled(false);
        } else {
            mBindPhoneBtn.setEnabled(true);
        }
    }

    private void doBindIdentityCardNumber() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_input_realname"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (TextUtils.isEmpty(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_number"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkIdentityCardNumber(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_correct"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        OnUserLoginActionListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doRealNameVerify( realName, idCardNumber);
        }
    }
    public interface OnUserLoginActionListener extends OnFinishActivityListener {

        void doRealNameVerify(String realName,String idCardNumber);
    }

    @Override
    public void onClick(View view) {
        if (view == mBindPhoneBtn) {
            doBindIdentityCardNumber();

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        WL.d("LoginBindDialogFragment", "onActivityResult ======> requestCode: " + requestCode);
    }



    private OnUserLoginActionListener getOnRealNameVerifyListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnUserLoginActionListener) {
                return (OnUserLoginActionListener) activity;
            }
        }
        return null;
    }




    private class ForceRealNameCertifiesBroadcastReceiver extends BroadcastReceiver {

        private boolean isIdcardPopupCloseEnable;

        public ForceRealNameCertifiesBroadcastReceiver() {
            this.isIdcardPopupCloseEnable = true;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.hasExtra("isIdcardPopupCloseEnable")) {
                this.isIdcardPopupCloseEnable = intent.getBooleanExtra("isIdcardPopupCloseEnable", true);
                WL.i(TAG, "onReceive ========> Value of the intent is " + this.isIdcardPopupCloseEnable);
            }
        }

        public boolean isIdcardPopupCloseEnable() {
            return isIdcardPopupCloseEnable;
        }
    }
}
